package ru.jexp.rcp.supply.view;

import java.util.List;

import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.swt.widgets.Composite;

import ru.jexp.obekt.identifikacia.IIdentificiruemii;
import ru.jexp.rcp.supply.actions.IPartCommands;
import ru.jexp.rcp.supply.viewer.JExpTableLabelProvider;
import ru.jexp.rcp.supply.viewer.JExpTableViewer;

public abstract class JExpViewPartTable<ViewType extends IIdentificiruemii>
		extends JExpViewPart implements IPartCommands {

	private String idEditor;
	private String nameEditor;

	protected abstract JExpTableLabelProvider createTableLabelProvider();
	
	public JExpViewPartTable(String idEditor, String nameView, String nameEditor) {
		super(nameView);
		this.idEditor = idEditor;
		this.nameEditor = nameEditor;

	}

	@Override
	public final String getIdEditor(IIdentificiruemii bean) {
		return idEditor;
	}

	@Override
	public final String getNameEditor(IIdentificiruemii bean) {
		return nameEditor;
	}

	@Override
	protected String[] acceptEditors() {
		return new String[] { idEditor };
	}

	@Override
	protected ColumnViewer createViwer(Composite parent) {
		return new JExpTableViewer(parent, createTableLabelProvider());
	}

	public void updateViewer(Object editBean, int action) {
		getTableViewer().updateViewer(action, prepareToViewer(editBean));
	}
	
	@Override
	protected IIdentificiruemii prepareToOpen(Object inViewer) {
		return (IIdentificiruemii) inViewer;
	}
	
	@Override
	protected IIdentificiruemii prepareToDelete(Object inViewer) {
		return (IIdentificiruemii) inViewer;
	}
	
	protected Object prepareToViewer(Object bean) {
		return bean;
	}

	protected final JExpTableViewer getTableViewer() {
		return (JExpTableViewer) getViewer();
	}

	@Override
	protected Object prepareToViewer(final List result) {
		return result;
	}
}